package org.locker.device.controller.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.locker.common.core.domain.R;
import org.locker.common.core.validate.AddGroup;
import org.locker.common.mybatis.core.page.PageQuery;
import org.locker.common.satoken.utils.LoginHelper;
import org.locker.common.web.core.BaseController;
import org.locker.domain.bo.FeedbackBo;
import org.locker.domain.enums.FeedbackStatusEnum;
import org.locker.domain.vo.FeedbackVo;
import org.locker.service.IFeedbackService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * (小程序)(投诉建议)
 *
 * @author lark
 * @date 2023-10-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/feedback")
public class WebFeedbackController extends BaseController {

    private final IFeedbackService feedbackService;

    /**
     * 查询反馈(投诉建议)列表
     */
    @GetMapping("/list")
    public R<Page<FeedbackVo>> list(FeedbackBo bo, PageQuery pageQuery) {
        Long userId = LoginHelper.getUserId();
        bo.setUserId(userId);
        return R.ok(feedbackService.queryPageList(bo, pageQuery));
    }



    /**
     * 获取反馈(投诉建议)详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<FeedbackVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(feedbackService.queryById(id));
    }

    /**
     * 新增反馈(投诉建议)
     */
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody FeedbackBo bo) {
        Long userId = LoginHelper.getUserId();
        bo.setUserId(userId);
        bo.setStatus(FeedbackStatusEnum.PENDING.getCode());
        return toAjax(feedbackService.insertByBo(bo));
    }

    /**
     * 删除反馈(投诉建议)
     *
     * @param id 主键
     */
    @DeleteMapping("/{id}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long id) {
        return toAjax(feedbackService.removeById(id));
    }
}
